package com.auto.learning.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.auto.learning.R;
import com.auto.learning.activity.VideoPlayActivity;
import com.auto.learning.bean.VideoBean;
import com.auto.learning.event.Handle;
import com.auto.learning.fragment.VideoPageFragment;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class VideoPageAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<VideoBean> list;
	Handle handler;
	
	DisplayImageOptions options;
	
	private int type = 0;  //0 为名人堂页面，1为搜索页面的显示
	
	public VideoPageAdapter(Context context,ArrayList<VideoBean> list,Handle handler){
		this.context = context;
		this.list = list;
		this.handler = handler;
		initImageLoader();
	}
	
	public VideoPageAdapter(Context context,ArrayList<VideoBean> list,Handle handler,int type){
		this.context = context;
		this.list = list;
		this.handler = handler;
		this.type = type;
		initImageLoader();
	}

	@Override
	public int getCount() {
		if (list == null) {
			return 0;
		}
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return list.get(position).getId();
	}

	ViewHolder holder;
	SearchViewHolder searchViewHolder;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final VideoBean bean = list.get(position);
		if (type == 0) {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.video_adapter, null);
				holder = new ViewHolder();
				holder.image_face = (ImageView) convertView.findViewById(R.id.image_face);
				holder.view_background = convertView.findViewById(R.id.view_background);
				holder.linear_bottom  = (RelativeLayout) convertView.findViewById(R.id.linear_bottom);
				holder.image_play = (ImageView) convertView.findViewById(R.id.image_play);
				holder.linear_share = (LinearLayout) convertView.findViewById(R.id.linear_share);
				holder.tv_look_num = (TextView) convertView.findViewById(R.id.tv_look_num);
				holder.tv_comment_num = (TextView) convertView.findViewById(R.id.tv_comment_num);
				convertView.setTag(holder);
			}else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv_look_num.setText(bean.getLookNum());
			holder.tv_comment_num.setText(bean.getCommentNum());
			holder.view_background.getBackground().setAlpha(180);
			holder.linear_bottom.getBackground().setAlpha(180);
			execAlpha(holder.view_background);
			ImageLoader.getInstance().displayImage(bean.getVideoImg(),holder.image_face,options);
			holder.image_play.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,VideoPlayActivity.class);

					intent.putExtra("m_bean", bean);
					context.startActivity(intent);
				}
			});
			
			holder.linear_share.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					handler.obtainMessage(VideoPageFragment.SHARE, bean).sendToTarget();
				}
			});
		}else if (type == 1) {
			if (convertView == null) {
				convertView = LayoutInflater.from(context).inflate(R.layout.search_mrt_adapter, null);
				searchViewHolder = new SearchViewHolder();
				searchViewHolder.image_face = (ImageView) convertView.findViewById(R.id.image_face);
				searchViewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
				searchViewHolder.tv_anthor = (TextView) convertView.findViewById(R.id.tv_anthor);
				searchViewHolder.tv_listen_number = (TextView) convertView.findViewById(R.id.tv_listen_number);
				convertView.setTag(searchViewHolder);
			}else {
				searchViewHolder = (SearchViewHolder) convertView.getTag();
			}
			ImageLoader.getInstance().displayImage(bean.getVideoImg(),searchViewHolder.image_face,options);
			searchViewHolder.tv_title.setText(bean.getTitle()+"");
			searchViewHolder.tv_anthor.setText(bean.getSlogan()+"");
			searchViewHolder.tv_listen_number.setText(bean.getLookNum()+"");
			convertView.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context,VideoPlayActivity.class);
					intent.putExtra("m_bean", bean);
					context.startActivity(intent);
				}
			});
			convertView.setBackgroundColor(context.getResources().getColor(R.color.main_backgroud_item));
		}
		return convertView;
	}
	
	private void execAlpha(View view){
		handler.postDelayed(new AlphaDel(1,view), 500);
	}
	
	class AlphaDel implements Runnable{
		
		int radio;
		View view;
		public AlphaDel(int radio,View view){
			this.radio = radio;
			this.view = view;
		}

		@Override
		public void run() {
			radio--;
			view.getBackground().setAlpha((radio * 10));
			if (radio == 0) {
				handler.removeCallbacks(this);
			}else {
				handler.postDelayed(new AlphaDel(radio,view), 200);
			}
		}
	}

	class ViewHolder{
		ImageView image_face,image_play;
		View view_background;
		RelativeLayout linear_bottom;
		LinearLayout linear_share;
		TextView tv_comment_num,tv_look_num;
	}
	
	class SearchViewHolder{
		ImageView image_face;
		TextView tv_title,tv_anthor,tv_listen_number;
	}
	
	private void initImageLoader(){
		options = new DisplayImageOptions.Builder()
		.showImageOnLoading(R.drawable.mrt)
		.showImageForEmptyUri(R.drawable.mrt)
		.showImageOnFail(R.drawable.mrt).cacheInMemory(true)
		.cacheOnDisk(true).considerExifParams(true)
		.bitmapConfig(Bitmap.Config.RGB_565).build();
	}
}
